#!/bin/bash

IP_DNS_2="10.11.3.68"
BROADCAST_DNS_2="10.11.3.63"
MASK_DNS_2="255.255.255.192"

#mascaras genericas
#MASK_24="255.255.255.0"
#MASK_25="255.255.255.128"
#MASK_26="255.255.255.192"
#MASK_27="255.255.255.224"
#MASK_28="255.255.255.240"
#MASK_29="255.255.255.248"
#MASK_30="255.255.255.252"

#redes (excluyendo a M y M')
#NETWORK_A="10.11.3.0" #/27
#NETWORK_B="10.34.13.0" #/24
#NETWORK_C="10.11.3.64" #/26
#NETWORK_D="10.11.2.0" #/24
#NETWORK_E="172.3.1.192" #/30
#NETWORK_F="10.11.3.32" #/27
#NETWORK_G="172.3.1.196" #/30
#NETWORK_H="10.34.1.0" #/24
#NETWORK_I="10.34.6.176" #/30
#NETWORK_J="172.3.1.200" #/30
#NETWORK_K="10.34.6.224" #/27
#NETWORK_L="10.34.6.180" #/30
#NETWORK_N="10.34.6.192" #/27
#NETWORK_O="10.34.6.144" #/28
#NETWORK_P="10.34.6.160" #/28
#NETWORK_Q="192.168.3.0" #/24
#NETWORK_R="10.34.6.128" #/28
#NETWORK_S="10.9.2.192" #/30

#deshabilita el forwarding porque los DNS no redireccionan paquetes.
#function no_forward {
#	echo "Deshabilitando el forwarding..."
#	
#	echo "0" >> /proc/sys/net/ipv4/ip_forward
#	returnCode=$( cat /proc/sys/net/ipv4/ip_forward )
#	if [ $returnCode -eq 0 ]; then
#		echo "Forwarding deshabilitado!"
#	else
#		echo "Error: No se pudo deshabilitar el forwarding!"
#	fi
#}

#function config_interface {
#	echo "Configurando la interfaz..."
#	
#	for i in $( ifconfig -a | grep eth | cut -d ' ' -f 1 | grep ':'); do
#		ifconfig $i down
#	done
#
#	#obtiene el nombre de la interfaz
#	interface=$( ifconfig -a | grep eth | cut -d ' ' -f 1 | grep -v ':' )
#
#	#elimina las rutas asociadas a la interfaz y vuelve a levantarla
#	ifconfig $interface down
#	ifconfig $interface up
#
#	ifconfig $interface $IP_DNS_2 broadcast $BROADCAST_DNS_2 netmask $MASK_DNS_2
#
#	echo "Fin de la configuracion de la interfaz!"
#
#	return $interface
#}

#function load_routing_table {
#	echo "Cargando la tabla de ruteo..."
#	
#	#falta terminar GW
#	route add -net "10.11.3.64/26" dev $interface
#	route add -net $RED_A netmask $MASK_27 gw "192.168.3.2" dev $interface
#	route add -net $RED_B netmask $MASK_24 gw "192.168.3.2" dev $interface
#	route add -net $RED_D netmask $MASK_24 gw "192.168.3.2" dev $interface
#	route add -net $RED_E netmask $MASK_30 gw "192.168.3.2" dev $interface
#	route add -net $RED_F netmask $MASK_27 gw "192.168.3.2" dev $interface
#	route add -net $RED_G netmask $MASK_30 gw "192.168.3.2" dev $interface
#	route add -net $RED_H netmask $MASK_24 gw "192.168.3.2" dev $interface
#	route add -net $RED_I netmask $MASK_30 gw "192.168.3.2" dev $interface
#	route add -net $RED_J netmask $MASK_30 gw "192.168.3.2" dev $interface
#	route add -net $RED_K netmask $MASK_27 gw "192.168.3.2" dev $interface
#	route add -net $RED_L netmask $MASK_30 gw "192.168.3.2" dev $interface
#	route add -net $RED_N netmask $MASK_27 gw "192.168.3.2" dev $interface
#	route add -net $RED_O netmask $MASK_28 gw "192.168.3.2" dev $interface
#	route add -net $RED_P netmask $MASK_28 gw "192.168.3.2" dev $interface
#	route add -net $RED_Q netmask $MASK_24 gw "192.168.3.2" dev $interface
#	route add -net $RED_R netmask $MASK_28 gw "192.168.3.2" dev $interface
#	route add -net $RED_S netmask $MASK_30 gw "192.168.3.2" dev $interface
#
#	echo "Fin de la carga de la tabla de ruteo!"
#}

#verifica si bind9 esta instalado y, en caso negativo, lo instala.
#function validate_bind9 {
#	echo "Verificando que bind9 este instalado..."	
#	
#	installed=`dpkg -s bind9|grep installed`
#	if [ "" == "$installed" ]; then
#		echo "bind9 no esta instalado!"
#		echo "Instalando bind9..."
#		sudo apt-get --force-yes --yes install bind9
#	else
#		echo "bind9 esta instalado!"
#	fi
#}

function import {
	echo "Vaciando la carpeta /etc/bind/..."
	sudo rm /etc/bind/*

	cd ./dns/dns2

	echo "Copiando el contenido en la carpeta /etc/bind/..."
	sudo cp ./* /etc/bind/

	cd ..

	sudo chmod 777 /etc/resolv.conf
	echo "Agregando la direccion IP de DNS2 al archivo /etc/resolv.conf..."
	echo "# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)" > /etc/resolv.conf
	echo "#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN" >> /etc/resolv.conf
	echo "domain salta.dc.fi.uba.ar" >> /etc/resolv.conf 
	echo "search salta.dc.fi.uba.ar" >> /etc/resolv.conf 
	echo "nameserver 127.0.0.1" >> /etc/resolv.conf
	echo "nameserver 10.11.3.68" >> /etc/resolv.conf
	sudo chmod 644 /etc/resolv.conf

	echo "Reiniciando bind9 con la nueva configuracion..."
	/etc/init.d/bind9 restart
}

##### MAIN #####

echo "DNS2: Iniciando configuracion..."
#no_forward
#config_interface
#load_routing_table
#validate_bind9
import
echo "DNS2: Configuracion finalizada!"
exit 0
